from rest_framework import serializers
from .models import City, DataCenter, Host, HostPassword, HostPingLog


class CitySerializer(serializers.ModelSerializer):
    """城市序列化器"""
    
    class Meta:
        model = City
        fields = '__all__'


class DataCenterSerializer(serializers.ModelSerializer):
    """机房序列化器"""
    city_name = serializers.CharField(source='city.name', read_only=True)
    
    class Meta:
        model = DataCenter
        fields = '__all__'


class DataCenterDetailSerializer(serializers.ModelSerializer):
    """机房详情序列化器"""
    city = CitySerializer(read_only=True)
    
    class Meta:
        model = DataCenter
        fields = '__all__'


class HostSerializer(serializers.ModelSerializer):
    """主机序列化器"""
    data_center_name = serializers.CharField(source='data_center.name', read_only=True)
    city_name = serializers.CharField(source='data_center.city.name', read_only=True)
    
    class Meta:
        model = Host
        fields = '__all__'


class HostDetailSerializer(serializers.ModelSerializer):
    """主机详情序列化器"""
    data_center = DataCenterDetailSerializer(read_only=True)
    
    class Meta:
        model = Host
        fields = '__all__'


class HostPasswordSerializer(serializers.ModelSerializer):
    """主机密码序列化器"""
    host_hostname = serializers.CharField(source='host.hostname', read_only=True)
    
    class Meta:
        model = HostPassword
        fields = ['id', 'host', 'host_hostname', 'password_changed_at', 'next_change_time']
        read_only_fields = ['encrypted_password', 'password_changed_at', 'next_change_time']


class HostPingLogSerializer(serializers.ModelSerializer):
    """主机ping日志序列化器"""
    host_hostname = serializers.CharField(source='host.hostname', read_only=True)
    host_ip = serializers.CharField(source='host.ip_address', read_only=True)
    
    class Meta:
        model = HostPingLog
        fields = '__all__'


class PingRequestSerializer(serializers.Serializer):
    """ping请求序列化器"""
    host_id = serializers.IntegerField(help_text='主机ID')
